const express = require('express');
const router = express.Router();
const db = require('../utils/db');

//渲染
router.post('/getAccount', (req, res) => {
  const { search, page, count } = req.body;
  const sql = `select * from student_account where name like '%${search}%' limit ${
    (page - 1) * count
  },${count}`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ code: 200, data, total: data.length });
  });
});
// 删除
router.delete('/deleteAccount', (req, res) => {
  const { id } = req.body;
  const sql = `delete  from student_account where id=${id}`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ code: 200, msg: '成功' });
  });
});

// 添加
router.post('/addAccount', (req, res) => {
  const { name, accountType, accountName, accountId, bank, tel, status } =
    req.body;
  const sql = `insert into student_account values (null,'${name}','${accountType}','${accountName}',${accountId},'${bank}',${tel},'${status}')`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ code: 200, msg: '成功' });
  });
});
// 修改
router.post('/updateAccount', (req, res) => {
  const {id, name, accountType, accountName, accountId, bank, tel, status } =
    req.body;
  const sql = `update student_account set name='${name}',accountType='${accountType}',accountName='${accountName}',accountId='${accountId}',bank='${bank}',tel='${tel}',status=${status} where id=${id};`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ code: 200, msg: '成功' });
  });
});

module.exports = router;
